package cn.wolfcode;

import cn.wolfcode.doamin.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class EmployeeTest2 {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    public void insert(){
        Employee employee = new Employee();
        employee.setAge(20);
        employee.setAdmin(0);
        employee.setDeptId(1L);
        employee.setEmail("app@qq.com");
        employee.setName("小妹");
        employee.setPassword("123");
        employeeMapper.insert(employee);
    }
    @Test
    public void updateById(){
        Employee employee = new Employee();
        employee.setId(1L);
        employee.setAdmin(employeeMapper.selectById(1L).getAdmin());
        employee.setAge(employeeMapper.selectById(1L).getAge());
        employee.setName("dafei");
        employeeMapper.updateById(employee);
    }
    @Test
    public void update(){

        UpdateWrapper<Employee> wrapper = new UpdateWrapper<>();
        wrapper.set("age",18).set("name","小笨蛋").eq("id",2L);
        employeeMapper.update(null,wrapper);

    }
    @Test
    public void deleteById(){
        employeeMapper.deleteById(3L);
    }
    @Test
    public void deleteBatchIds(){
       employeeMapper.deleteBatchIds(Arrays.asList(1L,2L));
    }
    @Test
    public void deleteByMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("name","钱总");
        map.put("password","1");
        employeeMapper.deleteByMap(map);
    }
    @Test
    public void delete(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("name","钱二明").eq("password","1");
        employeeMapper.delete(wrapper);

    }
    @Test
    public void selectById(){
        Employee employee = new Employee();
        employee.setId(6L);
        employeeMapper.selectById(employee.getId());
    }
    @Test
    public void selectBatchIds(){
        employeeMapper.selectBatchIds( Arrays.asList(6L,7L));
    }
    @Test
    public void selectByMap(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",15L);
        map.put("name","郑七明");
        employeeMapper.selectByMap(map);
    }
    @Test
    public void selectCount(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        employeeMapper.selectCount(wrapper);
    }
    @Test
    public void selectList(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        employeeMapper.selectList(wrapper);
    }
    @Test
    public void selectMaps(){

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        List<Map<String, Object>> map = employeeMapper.selectMaps(wrapper);
        map.forEach(System.out::println);

    }

    @Test
    public void selectPage(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        Page<Employee> page = new Page<>(1, 3);
        employeeMapper.selectPage(page,wrapper);
        System.out.println("当前页：" + page.getCurrent());

        System.out.println("每页显示条数：" + page.getSize());

        System.out.println("总页数：" + page.getPages());

        System.out.println("总数：" + page.getTotal());

        System.out.println("当前页数据：" + page.getRecords());
    }
    @Test
    public void selectOne(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("id",12L);
        Employee employee = employeeMapper.selectOne(wrapper);
        System.out.println(employee);
    }
    @Test
    public void selectObjs(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.select("name");
        employeeMapper.selectObjs(wrapper).forEach(System.out::println);
    }
    @Test
    public void selectMapsPage(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
       IPage<Map<String, Object>>  page= new Page<>(2,3);
        employeeMapper.selectMapsPage(page,wrapper);
    }
    @Test
    public void testUpdate(){
        UpdateWrapper<Employee> wrapper = new UpdateWrapper<>();
        wrapper.eq("name","郑总").eq("password","1").set("admin",1);
        employeeMapper.update(null,wrapper);
    }
    @Test
    public void testLambdaUpdate(){
        LambdaUpdateWrapper<Employee> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Employee::getAdmin,0).eq(Employee::getAge,19).setSql("name='孙子总'");
        employeeMapper.update(null,wrapper);
    }
    @Test
    public void testQueryWrapper(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.eq("name","孙子总");
        employeeMapper.selectOne(wrapper);
    }
    @Test
    public void testLambdaWrapper2(){
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getAge,25).eq(Employee::getName,"郑七明");
        employeeMapper.selectList(wrapper).forEach(System.out::println);
    }
    @Test
    public void testQuery(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.select("name","age");
        List<Employee> employees = employeeMapper.selectList(wrapper);
        System.out.println(employees);
    }
    @Test
    public void Query2(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("age");
        wrapper.orderByAsc("admin");
        List<Employee> employees = employeeMapper.selectList(wrapper);
        System.out.println(employees);
    }
    @Test
    public void Query3(){
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderBy(true,true, Employee::getAge, Employee::getAdmin);
        System.out.println(employeeMapper.selectList(wrapper));
    }
    @Test
    public void Query4(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.groupBy("dept_id");
        wrapper.select("dept_id","count(id) count").having("count>3");
        employeeMapper.selectMaps(wrapper);
    }
    @Test
    public void Query5(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.ne("name","吴总");
        employeeMapper.selectList(wrapper);
    }
    @Test
    public void testQuery6(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.notLike("name","吴六明");
        employeeMapper.selectList(wrapper);
    }
    @Test
    public void testQuery7(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","李五明");
        map.put("age",25);
        wrapper.allEq(map);
        System.out.println(employeeMapper.selectList(wrapper));
    }
}
